package com.polytech.ihm.factory;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.lang.reflect.Field;
import java.util.HashMap;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Form extends JPanel {
	private HashMap<String,JComponent> mapFields=new HashMap<String, JComponent>();
	private int labelWidth=0;
	public Form() {
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
	}
	public Form(Object value){
		this();
		try{
			Field[] fields = value.getClass().getDeclaredFields();
			for (Field field : fields) {
				String v="";
				if(field.getType().equals(String.class)){
					String getter=field.getName().substring(0, 1).toUpperCase()+field.getName().substring(1);
					 v=(String) value.getClass().getMethod("get"+getter).invoke(value);
				}
					FieldInformation fa = field.getAnnotation(FieldInformation.class);
					if(fa!=null){
						String fieldLabel=fa.labelName();
						if(fa.type()==FieldInformation.TYPE_STRING_FIELD)
						    addField(field.getName(),fieldLabel,v,20);
						if(fa.type()==FieldInformation.TYPE_PASSWORD)
							addPasswordField(field.getName(),fieldLabel,v,20);
						if(fa.type()==FieldInformation.TYPE_COMBOBOX){
							addComboBoxField(field.getName(),fieldLabel,fa.values());
						}
					}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public void addComboBoxField(String key, String fieldLabel,String[] values) {
		addComboBoxField(key, fieldLabel, values,100);
	}
	public void addComboBoxField(String key, String fieldLabel,String[] values,int width) {
		JComboBox<String> comboBox = new JComboBox<>(values);
		comboBox.setPreferredSize(new Dimension(width,comboBox.getPreferredSize().height));
		LabledComponent labledComponent=new LabledComponent(fieldLabel, comboBox);
		add(labledComponent);
		if(this.labelWidth!=labledComponent.getLabelWitdh()){
			setLabelWidth(labledComponent.getLabelWitdh());
		}
		mapFields.put(key, comboBox);
	}

	public void addField(String key,String value,int size){
		addField(key,key,value,size);
	}
	private void addField(String key, String fieldLabel,String value,int size) {
		JTextField textField = new JTextField(value,size);
		textField.setPreferredSize(new Dimension(textField.getPreferredSize().width,30));
		LabledComponent labledComponent=new LabledComponent(fieldLabel, textField);
		add(labledComponent);
		if(this.labelWidth!=labledComponent.getLabelWitdh()){
			setLabelWidth(labledComponent.getLabelWitdh());
		}
		mapFields.put(key, textField);
	}
	public void addPasswordField(String label, String value, int size) {
		addPasswordField(label, label, value, size);
	}
	private void addPasswordField(String key, String label, String value,int size) {
		JPasswordField passwordField = new JPasswordField(value,size);
		passwordField.setPreferredSize(new Dimension(passwordField.getPreferredSize().width,30));
		LabledComponent labledComponent=new LabledComponent(label, passwordField);
		add(labledComponent);
		if(this.labelWidth!=labledComponent.getLabelWitdh()){
			setLabelWidth(labledComponent.getLabelWitdh());
		}
		mapFields.put(key, passwordField);
	}
	
	public void addField(String key,int size){
		addField(key,"",size);
	}
	public void removeFiedl(String key){
		revalidate();
	}
	public void enableField(String label,boolean etat){

	}
	public void setFieldValue(String key,Object value){

	}
	public String getFieldValue(String id){
			if(mapFields.get(id) instanceof JTextField)
				return ((JTextField)mapFields.get(id)).getText();
			if(mapFields.get(id) instanceof JPasswordField)
				return new String(((JPasswordField)mapFields.get(id)).getPassword());
			
			return null;
	}
	public void reset (){
		
	}
	private void setLabelWidth(int labelWidth){
		if(this.labelWidth<labelWidth)
		  this.labelWidth=labelWidth;
		for(int i=0;i<getComponentCount();i++){
			Component c = getComponent(i);
			if(c instanceof LabledComponent){
				((LabledComponent)c).setLabelWitdh(this.labelWidth);
			}
		}
		revalidate();
		
	}
	public void addItemListener(String key,ItemListener itemListener){
		if(mapFields.get(key)instanceof JComboBox){
			JComboBox comboBox=(JComboBox)mapFields.get(key);
			 comboBox.addItem(itemListener);
		}
	}
	public void addActionListner(String key, ActionListener actionListener) {
		if(mapFields.get(key)instanceof JComboBox){
			JComboBox comboBox=(JComboBox)mapFields.get(key);
			comboBox.addActionListener(actionListener);
		}
	}
	public void setComboBoxValues(String key, String[] values) {
		if(mapFields.get(key)instanceof JComboBox){
			JComboBox comboBox=(JComboBox)mapFields.get(key);
			comboBox.removeAllItems();
			for(String s:values)
				comboBox.addItem(s);
		}
	}
	public String getComboBoxSelectedItem(String key) {
		if(mapFields.get(key)instanceof JComboBox){
			JComboBox comboBox=(JComboBox)mapFields.get(key);
			return (String) comboBox.getSelectedItem();
		}
		return null;
	}
	
}
